﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net.Sockets;
using System.Net;

/// <summary>
/// CSiteUtility의 요약 설명입니다.
/// </summary>
public class CSiteUtility
{
    public static void WriteLog(string title, string contents)
    {
        if (System.Configuration.ConfigurationSettings.AppSettings["DEBUG_MODE"] == "1")
        {
            try
            {
                string folderpath = System.Configuration.ConfigurationSettings.AppSettings["DEBUG_LOGPATH"];

                if (folderpath == "") 
                    return;

                string filepath = folderpath + "\\LOG[" + DateTime.Now.ToShortDateString() + "].txt";

                StreamWriter output = new StreamWriter(filepath, true, System.Text.Encoding.Default);
                output.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] " + title);
                output.WriteLine(contents);
                output.WriteLine();
                output.Close();
            }
            catch (Exception e) { }
        }
    }

    
    public static string GetLeftDate(string downlimitdate)
    {
        DateTime date1 = DateTime.Parse(DateTime.Now.ToShortDateString());
        DateTime date2 = DateTime.Parse(downlimitdate.Substring(0, 10));

        TimeSpan timeSpan = date2 - date1;

        if (timeSpan.Days < 0)
            return "0";
        else
            return timeSpan.Days.ToString();
    }

    // 실명 인증
    public static string CheckRegNo(HttpServerUtility server, string name, string regno)
    {
        string result = "";

        System.Text.Encoding euckr = System.Text.Encoding.GetEncoding("euc-kr");

        byte[] euckrbytes = euckr.GetBytes(name);

        name = "";

        foreach (byte b in euckrbytes)
        {
            name += "%" + string.Format("{0:X}", b);
        }

        string url = "http://www.interich.com/filetok/NameCheck.asp?sName=" + name + "&sJumin=" + regno;

        try
        {
            WebRequest myRequest = WebRequest.Create(url);

            myRequest.Timeout = 30000;
            WebResponse myResponse = myRequest.GetResponse();

            Stream ReceiveStream = myResponse.GetResponseStream();
            StreamReader readStream = new StreamReader(ReceiveStream);

            result = readStream.ReadToEnd().Trim();

            myResponse.Close();
        }
        catch (Exception e)
        {
            CSiteUtility.WriteLog(e.Message, e.StackTrace);
        }

        return result;
    }
}
